from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse, HttpResponse

from collect.models import Collect
from main.models import Article
from user.models import CustomUser


# Create your views here.

def collect(request):
    a_id = request.POST.get("article")
    u_id = request.POST.get("user")
    user = get_object_or_404(CustomUser, id=u_id)
    article = get_object_or_404(Article, id=a_id)
    collect = Collect.objects.filter(user=user, article=article).first()
    if collect is None:
        Collect.objects.create(user=user, article=article)
        return JsonResponse({'code': 0, 'status': 'success', 'msg': '收藏成功'})
    else:
        collect.delete()
        return JsonResponse({'code': 0, 'status': 'success', 'msg': '取消收藏成功'})
